/*
 * Copyright 2013-2019 Xia Jun(3979434@qq.com).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 ***************************************************************************************
 *                                                                                     *
 *                        Website : http://www.farsunset.com                           *
 *                                                                                     *
 ***************************************************************************************
 */
package com.netty.service;

import com.netty.constant.Constants;
import com.netty.constant.ImChannelGroup;
import com.netty.constant.ImChannels;
import com.netty.constant.ImChannels;
import com.netty.model.ReplyBody;
import com.netty.model.SentBody;
import io.netty.channel.Channel;
import io.netty.channel.group.ChannelGroup;
import org.assertj.core.util.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

/*
 * 消息发送实现类
 */
@Component
public class DefaultMessagePusher  {
    @Autowired
    private ImChannels imChannels;
    @Autowired
    private ImChannelGroup imChannelGroup;

    /**
     * 向用户发送消息
     * @param uid 用户唯一编码
     * @param sentBody 消息内容
     */
    public final void push(String uid,SentBody sentBody) {
        imChannels.writep2p(uid, sentBody);
    }
    /**
     * 发送给所有在线用户
     * @param sentBody
     */
    public final void pushAll(SentBody sentBody) {
        imChannels.writep2All(sentBody);
    }

    /**
     * 向组用户发送消息
     * @param groupId 分组id
     * @param sentBody 消息内容
     */
    public final void push2Group(String groupId, SentBody sentBody) {
        imChannelGroup.writep2Group(groupId,sentBody);
    }

    public final void addGroup(String userId, String groupId) throws Exception {
        Channel channel = imChannels.getChannel(userId);
        if(Objects.isNull(channel)) throw new Exception("当前用户不在线");
        imChannelGroup.addGroups(groupId,channel);
    }

    public List<Map<String,Object>> queryOnlineUser() {
        List<Map<String, Object>> all = imChannels.findAll();
        return all;
    }

    public List<Map<String,Object>> queryGroupOnlineUser(String groupId) throws Exception {
        ConcurrentHashMap<String, ChannelGroup> websocketGroup = imChannelGroup.getWebsocketGroup();
        ChannelGroup channels = websocketGroup.get(groupId);
        if(ObjectUtils.isEmpty(channels)) throw new Exception("当前分组不存在");
        List<Map<String,Object>> maps = new ArrayList<>();
        for (Channel key : channels) {
            Map<String,Object> map = new HashMap<>();
            map.put("channl",key.attr(Constants.SessionConfig.ID).get());
            map.put("user",key.attr(Constants.SessionConfig.UID).get());
            maps.add(map);
        }
        return maps;
    }
}
